/*
 * StartGame - initialize stuff at the start of the game
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.events.TimerEvent;
import flash.Lib;
import flash.display.Bitmap;
import flash.geom.Point;
import flash.geom.ColorTransform;
#end

import lib3f.LCG;

class StartGame
{

	#if flash9
	static public var fader : ColorTransform;
	static public var bgfader : Bitmap;
	#end
	
	static public var gamemode : Int;

	static public var playrecording : Bool = false;
	
	static public function start()
	{

		Game.cur = new Game(StartGame.gamemode);
		
		#if flash9
		FortressSprite.flags = new Array();
		#end
		
		MapGen.entrypoints = new Array();
		MapGen.castles = new Array();
						
		/*if (playrecording && !(Recorder.mouserecording==null))
		{
			Recorder.playback();
			LCG.seed = Recorder.recordingheader.get("seed");
		}
		else
		{
			#if flash9
				Recorder.startrecord();
			#else neko
				trace("couldn't play recording!?");
				neko.Sys.exit(1);
			#end
		}*/

		MapGen.genmap();							   
		Main.bg.build();
		
		for (n in 0...3)
		{
			PathFinder.genpaths(MapGen.castles[n], n, false);
		}
		
		for (n in 0...3)
		{
			Game.cur.creepman.shore.push(PathFinder.genpaths(MapGen.entrypoints[n], n, true));
		}

		#if flash9
		NetworkDialogue.bringup();
		ComponentManager.add(Restarter); // we do not save recordings unless they reach an endgame?
		#end
		
	}	

	static public function initgraphics()
	{
		#if flash9
			Main.bg = new TileBG();
			Main.bg.visible = false;
			
			bgfader = new Bitmap();
			Lib.current.addChild(Main.bg);
			Lib.current.addChild(bgfader);
			IsoManager.sprite.visible = false;
			ComponentManager.add(IsoManager);
			ComponentManager.add(PopTart);
			
			fader = new ColorTransform(1,1,1,0);
		#end	
	}
	
	static public function up()
	{
		// fade in

		#if flash9
		fader.alphaMultiplier+=0.03;
		bgfader.bitmapData = Main.bg.bitmapData.clone();
		bgfader.bitmapData.colorTransform(bgfader.bitmapData.rect, fader);		
		if (fader.alphaMultiplier >= 1)
		{
			bgfader.visible = false;
			Main.bg.visible = true;
			IsoManager.sprite.visible = true;
			ComponentManager.remove(StartGame);
		}
		#else neko
			ComponentManager.remove(StartGame);
		#end
	}
	
	static public function end()
	{
		Game.cur.phase = 1;
		FortressManager.calcterritory();
	}
	
}
